package com.hk.dao.impl;

import com.hk.dao.INewsDao;
import com.hk.model.News;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.ResultSet;

/**
 * @Author: zhe
 * @Date: 2021/6/16 10:31
 * @Statement: 新闻Dao层接口实现类
 */
public class NewsDaoImpl implements INewsDao {

    //    JDBC连接工具类
    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();
    private static String sql = "";

    @Override
    public ResultSet findAll(Integer page, Integer limit, News t) {
        sql = "select * from news where 1=1 ";
        if (!t.getTitle().equals("")) {
            sql += " and title like "+ SqlSymbol.MARK.getValue()+SqlSymbol.PERCENT.getValue()+t.getTitle()+SqlSymbol.PERCENT.getValue()+SqlSymbol.MARK.getValue();
        }
        if (t.getTypeId() > 0) {
            sql += " and type_id = "+t.getTypeId();
        }
        if (!t.getReleaseDate().equals("")) {
            sql += " and release_date = "+SqlSymbol.MARK.getValue()+t.getReleaseDate()+SqlSymbol.MARK.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit, News t) {
        sql = "select count(1) from news where 1=1 ";
        if (!t.getTitle().equals("")) {
            sql += " and title like "+ SqlSymbol.MARK.getValue()+SqlSymbol.PERCENT.getValue()+t.getTitle()+SqlSymbol.PERCENT.getValue()+SqlSymbol.MARK.getValue();
        }
        if (t.getTypeId() > 0) {
            sql += " and type_id = "+t.getTypeId();
        }
        if (!t.getReleaseDate().equals("")) {
            sql += " and release_date = "+SqlSymbol.MARK.getValue()+t.getReleaseDate()+SqlSymbol.MARK.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from news where id = "+id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(News t) {
        sql = "insert into news value " +
                "("+null+ SqlSymbol.COMMA.getValue()+SqlSymbol.MARK.getValue()+t.getTitle()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getSubhead()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getContent()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getReleaseDate()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getAuthor()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                t.getHits()+SqlSymbol.COMMA.getValue()+
                t.getTypeId()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                SqlSymbol.RBRACKET.getValue();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int update(News t) {
        sql = "update news set " +
                "title = "+SqlSymbol.MARK.getValue()+t.getTitle()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "subhead = "+SqlSymbol.MARK.getValue()+t.getSubhead()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "content = "+SqlSymbol.MARK.getValue()+t.getContent()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "author = "+SqlSymbol.MARK.getValue()+t.getAuthor()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "type_id = "+t.getTypeId()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int delete(Integer id) {
        sql = "delete from news where id = "+id;
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }
}
